package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql" //需用到init()方法
	"github.com/jmoiron/sqlx"
)

type user struct {
	Id int  `json:"id"`
	Name string  `json:"name"`
}

func DB() *sqlx.DB {
	db, _:=sqlx.Connect("mysql","root:mysql@tcp(127.0.0.1:3306)/gotest")
	return db
}

func main() {
	db:=DB()
	//查询
	sqlStr:="select id, name from user where id=2"
	var u user
	err:=db.Get(&u, sqlStr)
	if err!=nil{
		fmt.Println("查询错误",err)
	}
	fmt.Println(u)
	fmt.Println(u.Id, u.Name)

	//查询多条
	sqlStr="select id, name from user"
	var users []user
	err=db.Select(&users, sqlStr)
	if err!=nil{
		fmt.Println("查询错误",err)
	}
	fmt.Println(users)

	//增删改和原生一样

}